package me.zhengjie.modules.device.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.device.domain.AccessDevice;
import me.zhengjie.modules.device.service.AccessDeviceService;
import me.zhengjie.modules.device.service.dto.AccessDeviceDTO;
import me.zhengjie.modules.device.service.query.AccessDeviceQueryService;
import me.zhengjie.modules.system.service.AreaService;
import me.zhengjie.modules.system.service.dto.AreaDTO;
import me.zhengjie.modules.system.service.dto.DictDetailDTO;
import me.zhengjie.modules.system.service.query.AreaQueryService;
import me.zhengjie.modules.system.service.query.DictDetailQueryService;
import me.zhengjie.utils.SecurityContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 张鹏
 * @date 2019-05-07
 */
@RestController
@RequestMapping("api")
public class AccessDeviceController {

    @Autowired
    private AccessDeviceService accessDeviceService;

    @Autowired
    private AreaQueryService areaQueryService;

    @Autowired
    private AccessDeviceQueryService accessDeviceQueryService;

    @Autowired
    private DictDetailQueryService dictDetailQueryService;

    private static final String ENTITY_NAME = "accessDevice";

    @Log("查询AccessDevice")
    @GetMapping(value = "/accessDevice")
    public ResponseEntity getAccessDevices(AccessDeviceDTO resources, Pageable pageable) {
        return new ResponseEntity(accessDeviceQueryService.queryAll(resources, pageable), HttpStatus.OK);
    }

    @Log("新增AccessDevice")
    @PostMapping(value = "/accessDevice")
    public ResponseEntity create(@Validated @RequestBody AccessDevice resources) {
        if (resources.getId() != null) {
            throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        UserDetails userDetails = SecurityContextHolder.getUserDetails();
        resources.setCreator(userDetails.getUsername());
        resources.setUpdator(userDetails.getUsername());
        Date now = new Date();
        resources.setCreateTime(new Timestamp(now.getTime()));
        resources.setUpdateTime(new Timestamp(now.getTime()));
        return new ResponseEntity(accessDeviceService.create(resources), HttpStatus.CREATED);
    }

    @Log("修改AccessDevice")
    @PutMapping(value = "/accessDevice")
    public ResponseEntity update(@Validated @RequestBody AccessDevice resources) {
        if (resources.getId() == null) {
            throw new BadRequestException(ENTITY_NAME + " ID Can not be empty");
        }
        UserDetails userDetails = SecurityContextHolder.getUserDetails();
        resources.setUpdator(userDetails.getUsername());
        Date now = new Date();
        resources.setUpdateTime(new Timestamp(now.getTime()));
        accessDeviceService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除AccessDevice")
    @DeleteMapping(value = "/accessDevice/{id}")
    public ResponseEntity delete(@PathVariable Long id) {
        accessDeviceService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @Log("获取AccessDevice所需字典")
    @GetMapping(value = "/accessDevice/dict")
    public ResponseEntity getDict(@PageableDefault(value = 10, sort = {"sort"}, direction = Sort.Direction.ASC) Pageable pageable) {
        Map<String, Object> map = new HashMap<>();
        map.put("area", areaQueryService.queryAll(new AreaDTO()));
        DictDetailDTO isNoDto = new DictDetailDTO();
        isNoDto.setDictName("is_no");
        Object isNoDtoObject = dictDetailQueryService.queryAll(isNoDto);
        map.put("is_no", isNoDtoObject);
        DictDetailDTO inOutFlagDto = new DictDetailDTO();
        inOutFlagDto.setDictName("in_out_flag");
        map.put("in_out_flag", dictDetailQueryService.queryAll(inOutFlagDto));
        return new ResponseEntity(map, HttpStatus.OK);
    }
}